from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser


@admin.register(CustomUser)
class CustomUserAdmin(UserAdmin):
    """自定义用户管理"""

    # 列表显示字段
    list_display = [
        'username', 'email', 'phone', 'location',
        'is_staff', 'is_active', 'date_joined'
    ]

    # 列表过滤
    list_filter = [
        'is_staff', 'is_active', 'date_joined', 'location'
    ]

    # 搜索字段
    search_fields = [
        'username', 'email', 'phone', 'location', 'bio'
    ]

    # 排序
    ordering = ['-date_joined']

    # 分页
    list_per_page = 20

    # 字段分组
    fieldsets = (
        ('基本信息', {
            'fields': ('username', 'password', 'email', 'phone')
        }),
        ('个人资料', {
            'fields': ('avatar', 'bio', 'location')
        }),
        ('权限状态', {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')
        }),
        ('重要日期', {
            'fields': ('last_login', 'date_joined')
        }),
    )

    # 添加用户时的字段
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'phone', 'password1', 'password2'),
        }),
    )

    # 只读字段
    readonly_fields = ['last_login', 'date_joined']

    # 自定义方法
    def get_user_attractions_count(self, obj):
        return obj.created_attractions.count()

    get_user_attractions_count.short_description = '创建景点数'

    def get_user_comments_count(self, obj):
        return obj.sentimentcomment_set.count()

    get_user_comments_count.short_description = '评论数'

    def get_user_collections_count(self, obj):
        return obj.collects.count()

    get_user_collections_count.short_description = '收藏数'

# 删除或注释掉不需要的模型注册
# @admin.register(Design)  # 如果Design模型不再需要，可以删除这行
# class DesignAdmin(admin.ModelAdmin):
#     pass